//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    activeIndex: 0,
    hidden: false,
    cou_index: 0,
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },

  onLoad: function () {
    var that = this;
    // —————————————— 获取网址——————————
    app.util.request({
      url: "entry/wxapp/Attachurl",
      'cachetime': '0',
      success: function (res) {
        that.setData({
          url: res.data
        })
      }
    })

    // wx.getSystemInfo({
    //   success: function (res) {
    //     console.log(res.model)
    //     console.log(res.pixelRatio)
    //     console.log(res.windowWidth)
    //     console.log(res.windowHeight)
    //     console.log(res.language)
    //     console.log(res.version)
    //     console.log(res.platform)
    //   }
    // })

    // ——————————————幻灯片 ——————————
    app.util.request({
      url: 'entry/wxapp/GetSlide',
      'cachetime': '0',
      success: function (res) {
        console.log("幻灯片")
        console.log(res.data)
        if (res.data.code == 200) {
          that.setData({
            imgUrls: res.data.list,
          })
        } else {
          that.setData({
            imgUrls: [],
          })
        }

      },
    })

    // ——————————————头条 ——————————
    app.util.request({
      url: 'entry/wxapp/Headline',
      'cachetime': '0',
      success: function (res) {
        console.log("头条")
        console.log(res.data)
        if (res.data.code == 200) {
          that.setData({
            title: res.data.list,
          })
        } else {
          that.setData({
            title: [],
          })
        }

      },
    })

    var user_id = wx.getStorageSync('user_msg').id
    var schoolid = wx.getStorageSync('user_msg').schoolid

    // ——————————————课程级别 ——————————
    app.util.request({
      url: 'entry/wxapp/CourseCates',
      'cachetime': '0',
      data:{
        user_id: user_id,
        school_id: schoolid
      },
      success: function (res) {
        console.log("级别")
        console.log(res.data)
        if (res.data.code == 501) {
          // that.setData({
          //   hidden: true,
          // })
        } else {
          that.setData({
            level: res.data.list,
            hidden: true,
          })
          that.order()
        }
      },
    })
  },

  //获取授权
  // bindGetUserInfo: function (e) {
  //   wx.setStorageSync("user_info", e.detail.userInfo)
  //   console.log(e.detail.userInfo)
  //   wx.getUserInfo({
  //     success: function (res) {
  //       console.log(res.userInfo)
  //     },
  //     fail: function (res) {
  //       console.log("点击取消")
  //       wx.showToast({
  //         title: '只有点击允许才能交作业!',
  //         icon: 'none',
  //         duration: 2000
  //       })
  //       setTimeout(function(){
  //         wx.navigateBack({
  //           delta: 1
  //         })
  //       },2000)
  //     }
  //   })
  // },

  //获取课程列表
  order: function (e) {
    var that = this;
    console.log(that)
    console.log(that.data.level)
    var cou_index = that.data.cou_index
    var first_id = that.data.level[cou_index].id
    var activeIndex = that.data.activeIndex
    console.log(first_id)
    // ——————————————课程 ——————————
    app.util.request({
      url: 'entry/wxapp/Course',
      'cachetime': '0',
      data: {
        id: first_id
      },
      success: function (res) {
        console.log("级别详情")
        console.log(res.data)
        // console.log(that)
        if (res.data.code == 501) {
          that.setData({
            none: res.data
          })
        } else {
          that.setData({
            con: res.data.list,
            none: res.data
          })
        }
      },
    })
  },

  /*点击课程选择 */
  onDetail: function (e) {
    var that = this;
    console.log(e)
    console.log(that.data)
    var detail_id = e.currentTarget.dataset.id
    var activeIndex = that.data.activeIndex;
    var index = e.currentTarget.dataset.index;
    var cou_index = e.currentTarget.dataset.index;
    that.setData({
      cou_index: cou_index
    })
    that.order()
    that.setData({
      index: index,
      activeIndex: index
    })

  },

  /*进入排名 */
  onRank:function(e){
    var return_con = wx.getStorageSync('user_msg')
    console.log(return_con)
    if (return_con == "") {
      wx.showModal({
        title: '提示',
        content: '您还没有登录,请先登录!',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx: wx.navigateTo({
              url: '../rank/login_firset',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      var u_id = wx.getStorageSync('user_msg').id
      console.log(u_id)
      app.util.request({
        url: "entry/wxapp/IsLogin",
        'cachetime': '0',
        data: {
          u_id: u_id
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 501) {
            wx.showModal({
              title: '提示',
              content: '该用户已被注销,请重新注册!',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx: wx.navigateTo({
                    url: '../rank/login',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx: wx.redirectTo({
              url: '../rank/rank',
            })
          }
        }
      })
    }
  },

  /*进入我的 */
  onMy: function (e) {
    var return_con = wx.getStorageSync('user_msg')
    console.log(return_con)
    if (return_con == "") {
      wx.showModal({
        title: '提示',
        content: '您还没有登录,请先登录!',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx: wx.navigateTo({
              url: '../rank/login_firset',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      var u_id = wx.getStorageSync('user_msg').id
      console.log(u_id)
      app.util.request({
        url: "entry/wxapp/IsLogin",
        'cachetime': '0',
        data: {
          u_id: u_id
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 501) {
            wx.showModal({
              title: '提示',
              content: '该用户已被注销,请重新注册!',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx: wx.navigateTo({
                    url: '../rank/login',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx: wx.redirectTo({
              url: '../my/my',
            })
          }
        }
      })
    }
  },

  /*进入日期列表*/
  task_list: function (e) {
    var that = this;
    console.log(e)
    console.log(that)
    var cour_id = e.currentTarget.dataset.id
    var return_con = wx.getStorageSync('user_msg')
    console.log(return_con)
    if (return_con == "") {
      wx.showModal({
        title: '提示',
        content: '您还没有登录,请先登录!',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx: wx.navigateTo({
              url: '../rank/login_firset',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      console.log(e)
      var that = this;
      var task_id = that.data.first_id
      console.log(that)
      console.log(task_id)
      var index = e.currentTarget.dataset.index
      var u_id = wx.getStorageSync('user_msg').id
      console.log(u_id)
      app.util.request({
        url: "entry/wxapp/IsLogin",
        'cachetime': '0',
        data: {
          u_id: u_id
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 501) {
            wx.showModal({
              title: '提示',
              content: '该用户已被注销,请重新注册!',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx: wx.navigateTo({
                    url: '../rank/login_firset',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx: wx.navigateTo({
              // url: 'task_detail?task_id=' + task_id + "&index=" + index + "&u_id=" + u_id,
              url: 'date_list?cour_id=' + cour_id,

            })
          }
        },
      })
    }
  },

  //点击进入详情
  onAticle: function (e) {
    var article_id = e.currentTarget.dataset.id;
    console.log(article_id)
    wx: wx.navigateTo({
      url: '../my/article_detail?articles_id=' + article_id,
    })
  },

  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {
    // var that = this;
    // that.order()   
    // wx.clearStorage() 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.order()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})